﻿using Ch9_Portable.Models;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Serialization;

namespace Ch9_Portable.Services
{
    public class Ch9RssService
    {
        //Featured - http://channel9.msdn.com/Feeds/RSS

        // Shows - http://channel9.msdn.com/Browse/Shows/RSS

        // Series - http://channel9.msdn.com/Browse/Series/RSS

        //http://channel9.msdn.com/Events/Pure-Imagination/2012/RSS
        //http://channel9.msdn.com/Events/Pure-Imagination/2012/RSS/mp4
        //http://channel9.msdn.com/Events/Pure-Imagination/2012/RSS/mp4high
        //http://channel9.msdn.com/Events/Pure-Imagination/2012/RSS/wmv
        //http://channel9.msdn.com/Events/Pure-Imagination/2012/RSS/wmvhigh
        //http://channel9.msdn.com/Events/Pure-Imagination/2012/RSS/zune
        //http://channel9.msdn.com/Events/Pure-Imagination/2012/RSS/mp3

        protected static string baseUrl = "http://channel9.msdn.com/";

        public static async Task<Ch9Rss> GetFeaturedShowsAsync()
        {
            try
            {
                Ch9Rss featuredShows = await GetCh9ContentAsync(baseUrl + "Feeds/RSS");
                return featuredShows;
            }
            catch (Exception e)
            {
                Debug.WriteLine(e.Message);
                return null;
            }
        }

        public static async Task<Ch9Rss> GetSeriesListAsync()
        {
            try
            {
                Ch9Rss seriesShows = await GetCh9ContentAsync(baseUrl + "Browse/Series/RSS");
                return seriesShows;
            }
            catch (Exception e)
            {
                Debug.WriteLine(e.Message);
                return null;
            }
        }

        public static async Task<Ch9Rss> GetShowListAsync()
        {
            try
            {
                Ch9Rss seriesShows = await GetCh9ContentAsync(baseUrl + "Browse/Shows/RSS");
                return seriesShows;
            }
            catch (Exception e)
            {
                Debug.WriteLine(e.Message);
                return null;
            }
        }

        public static async Task<Ch9Rss> GetTaggedShowsAsync(string tag)
        {
            try
            {
                Ch9Rss taggedShows = await GetCh9ContentAsync(baseUrl + "Tags/" + tag + "/RSS");
                return taggedShows;
            }
            catch (Exception e)
            {
                Debug.WriteLine(e.Message);
                return null;
            }
        }

        public static async Task<Ch9Rss> GetCh9ContentAsync(string ch9RSS)
        {
            try
            {
                var httpClient = new HttpClient();
                Debug.WriteLine(ch9RSS);
                string response = await httpClient.GetStringAsync(ch9RSS);
                StringReader stringRead = new StringReader(response);
                XmlSerializer xmlS = new XmlSerializer(typeof(Ch9Rss));
                XmlReader reader = XmlReader.Create(stringRead);
                Ch9Rss result = (Ch9Rss)xmlS.Deserialize(reader);
                return result;

            }
            catch (Exception e)
            {
                Debug.WriteLine(e.Message);
                throw e;
            }
        }

        public static async Task<Ch9Rss> GetShowAsync(string showURL)
        {
            try
            {
                Ch9Rss show = await GetCh9ContentAsync(showURL);
                return show;
            }
            catch (Exception e)
            {
                Debug.WriteLine(e.Message);
                return null;
            }
        }

        public static async Task<Ch9Rss> GetSeriesAsync(string seriesURL)
        {
            try
            {
                Ch9Rss series = await GetCh9ContentAsync(seriesURL);
                return series;
            }
            catch (Exception e)
            {
                Debug.WriteLine(e.Message);
                return null;
            }
        }
    }
}
